Programming Games with Microsoft Small Basic by Philip Conrod & Lou Tylee

Programming Games with Microsoft Small Basic by Philip Conrod & Lou Tylee

Author:Philip Conrod & Lou Tylee [Conrod, Philip]
Language: eng
Format: azw3
ISBN: 9781937161989
Publisher: Kidware Software LLC
Published: 2017-04-20T04:00:00+00:00


To detect mouse clicks, we need this line at the end of InitializeProgram:

GraphicsWindow.MouseDown = MouseDownSub

With this, each time a MouseDown event occurs, the subroutine named MouseDownSub is called. The code to perform the steps above will be in this subroutine.

Add this subroutine (MouseDownSub) to your program (this implements all the above steps):

Sub MouseDownSub

x = GraphicsWindow.MouseX

y = GraphicsWindow.MouseY

If (GameStatus = "Playing") Then

If (CarMoving = "false" And LoadVisible = "false") Then

DeliveryR = 1 + Math.Floor((y ‐ DeliveryGridY[1][1]) / GridW)

DeliveryC = 1 + Math.Floor((x ‐ DeliveryGridX[1][1]) / GridW)

If (DeliveryR < 1 Or DeliveryR > 20 Or DeliveryC < 1 Or DeliveryC > 20) Then

Goto LeaveMouseDownSub

EndIf

DeltaR = DeliveryR ‐ CarR

DeltaC = DeliveryC ‐ CarC

If (DeltaR <> 0 Or DeltaC <> 0) Then

Message1 = "Car Going To:"

Message2 = DeliveryR + " " + Text.GetCharacter(DeliveryC + 64)

DisplayMessage() CarMoving = "true"

EndIf

EndIf

EndIf

LeaveMouseDownSub:

EndSub

Look at the math that converts a clicked (x, y) coordinate to row and column values. We simply determine the distance of the clicked point from the grid border in each direction and divide that by the width of a grid square. The Math.Floor method knocks off any decimal result to give us a whole number result. It’s pretty cool!

Save and Run the game. Click Start Game, click Load. You should now be able to click the grid and have the clicked location appear in the message area. Here’s my window after I clicked the square in Row 14, Column R:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.